package com.workcenter.app;

import java.util.Date;

import android.R.string;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;

import com.ppcodes.core.utils.UtiDialogsFactory;
import com.workcenter.base.UIBase;
import com.workcenter.entity.Session;
import com.workcenter.entity.model.ESearchCondition;

public class UIFilter extends UIBase
{
   private TextView txtTitle;

   private TextView txtBeginDate;
   private TextView txtEndDate;
   private TextView txtProgress;

   private TableRow trProgress;

   private EditText edtCustomerName;
   private EditText edtRepairsId;
   
   private Button btnOK;
   private Button btnCancel;

   private void InitControls()
   {
	  edtCustomerName=(EditText)findViewById(R.id.edtCustomerName_filter);
	  edtRepairsId=(EditText)findViewById(R.id.edtOrderNumber_filter);
	  
	  txtTitle = (TextView) findViewById(R.id.txtTitle_toptoolbar_no_btn);
	  txtTitle.setText(getString(R.string.title_filter));
	  
	  
	  Date time = new Date();
	  String monthS=String.valueOf(time.getMonth() + 1);
	  String dayS=String.valueOf(time.getDate());
	  txtBeginDate = (TextView) findViewById(R.id.txtBeginDate_filter);
	  txtBeginDate.setText(String.format("%s-%s-%s", String.valueOf(time.getYear()+1900), monthS.length()==1?("0"+monthS):monthS, dayS.length()==1?("0"+dayS):dayS));
	  txtBeginDate.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			final DatePicker picker = new DatePicker(UIFilter.this);
			UtiDialogsFactory.getInstance().ShowCustomViewDialogWithOKCancel(UIFilter.this, null, picker, new DialogInterface.OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				  String month=String.valueOf(picker.getMonth() + 1);
				  String day=String.valueOf(picker.getDayOfMonth());
				  txtBeginDate.setText(String.format("%s-%s-%s", String.valueOf(picker.getYear()), month.length()==1?("0"+month):month, day.length()==1?("0"+day):day));
				  dialog.dismiss();
			   }
			});
		 }
	  });
	  txtEndDate = (TextView) findViewById(R.id.txtEndDate_filter);
	  txtEndDate.setText(String.format("%s-%s-%s", String.valueOf(time.getYear()+1900), monthS.length()==1?("0"+monthS):monthS, dayS.length()==1?("0"+dayS):dayS));
	  txtEndDate.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			final DatePicker picker = new DatePicker(UIFilter.this);
			UtiDialogsFactory.getInstance().ShowCustomViewDialogWithOKCancel(UIFilter.this, null, picker, new DialogInterface.OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				  String month=String.valueOf(picker.getMonth() + 1);
				  String day=String.valueOf(picker.getDayOfMonth());
				  txtEndDate.setText(String.format("%s-%s-%s", String.valueOf(picker.getYear()), month.length()==1?("0"+month):month, day.length()==1?("0"+day):day));
				  dialog.dismiss();
			   }
			});
		 }
	  });
	  
	  
	  
	  txtProgress = (TextView) findViewById(R.id.txtProgress_filter);
	  txtProgress.setText("全部");
	  trProgress = (TableRow) findViewById(R.id.trProgress_filter);
	  trProgress.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			final String[] items = { "全部", "待处理", "已派工", "未完成", "已完成" };
			UtiDialogsFactory.getInstance().ShowSingleSelectItemDialog(UIFilter.this, items, 0, new DialogInterface.OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				  txtProgress.setText(items[which]);
				  dialog.dismiss();
			   }
			});
		 }
	  });

	  btnOK = (Button) findViewById(R.id.btnOK_filter);
	  btnOK.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			setResult(RESULT_OK);
			Session.getInstance().setSearchCondition(new ESearchCondition(
				  edtRepairsId.getText().toString().trim(), 
				  edtCustomerName.getText().toString().trim(),
				  txtBeginDate.getText().toString(), 
				  txtEndDate.getText().toString(),
				  txtProgress.getText().toString()));
			finish();
		 }
	  });

	  btnCancel = (Button) findViewById(R.id.btnCancel_filter);
	  btnCancel.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			setResult(RESULT_CANCELED);
			finish();
		 }
	  });
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.filter);
	  InitControls();
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == event.KEYCODE_BACK)
	  {
		 setResult(RESULT_CANCELED);
		 finish();
	  }
	  return false;
   }
}
